var listUrl = rootPath + '/manage/examPaper/papers/paperAlreadyUploadUI';
var deleteUrl = rootPath + '/manage/examPaper/papers/deletePaper/exec';
var clearCachedUrl = rootPath + '/manage/examPaper/papers/clearCached/exec';
var detailUrl = rootPath + '/manage/examPaper/papers/detailUI';
$(function(){
	//批量删除
	$('.btn-moreDelete').click(function(){
		//获取复选框
		var $select=$("input:checked[name='checkbox_dj']");
		//判断有没有选择
		if($select.length==0){
			layer.msg('请选择要删除的记录！');
			return false;
		}
		
		//循环复选框所有记录值
		var ids = [];
		$($select).each(function(){
	       ids.push(this.value);
	    });
		
		deleteFun(ids,2,'您确定要删除所选答卷吗？');
	});
	
	//单条删除
	$('.btn-delete').click(function(){
		var ids = [];
		ids.push($(this).parents('tr').attr('data-id'));
		deleteFun(ids,1,'您确定要删除该答卷吗？');
	});
	
	//清空答卷
	$('.btn-clear').click(function(){
		clearFun(0);
	});
	
	//清空转换的临时文件缓存
	$('.btn-clearCached').click(function(){
		clearFun(1);
	});
	
	//查看答卷文件
	$('.btn-detail').click(function(){
		var id = $(this).parents('tr').attr('data-id');
		var tempId = $(this).parents('tr').attr('data-tempid');
		viewPaper(id,tempId);
	});
	
	//全选、反选
	$("#checkbox_djAll").click(function () {
		var checkedOfAll=$("#checkbox_djAll").prop("checked"); 
		$("input[name='checkbox_dj']").prop("checked", checkedOfAll); 
	});
});

var deleteFun = function(ids,type,msg){
	layer.confirm(msg, function(index) {
		common_ajax.ajax(deleteUrl, {
			batchId : batchId,
			gradeNo : gradeNo,
			courseNo : courseNo,
			ids : ids.join(';'),
			type : type
		}, 'json', function(data){
			if(data.resultCode=='000000'){
				layer.msg('操作成功！');
				setTimeout(function(){
					refresh();
				}, 1000);
			}else{
				layer.alert('操作失败！',{icon:5});
			}
		});
	});
}

var clearFun = function(type){
	var $select=$("input[name='checkbox_dj']");
	//判断有没有选择
	if($select.length==0){
		layer.msg('没有记录！');
		return false;
	}
	common_ajax.ajax(clearCachedUrl, {
		batchId : batchId,
		gradeNo : gradeNo,
		courseNo : courseNo,
		type : type
	}, 'json', function(data){
		if(data.resultCode=='000000'){
			layer.msg('操作成功！');
			setTimeout(function(){
				refresh();
			}, 1000);
		}else{
			layer.alert('操作失败！',{icon:5});
		}
	});
}

//查看科目答卷原图
var viewPaper = function(id,courseTemplateId){
	var pageii = layer.open({
		title : '科目答卷',
		type : 2,
		content : detailUrl + '?id='+id+'&courseTemplateId='+courseTemplateId+'&screen_width='+document.documentElement.clientWidth
	});
	layer.full(pageii);
}

//刷新页面
function refresh(){
	location.href = listUrl + '?batchId='+batchId+'&gradeNo='+gradeNo+'&courseNo='+courseNo;
}